﻿using Abp.Hangfire;
using Abp.Hangfire.Configuration;
using Abp.Modules;
using AVIS.OutBound;
using Hangfire;
using Hangfire.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.TaskScheduler.ServerNode
{
    [DependsOn(
        typeof(AbpHangfireModule),
        typeof(OutBoundDataModule))]
    public class TaskSchedulerServerNodeModule : Abp.Modules.AbpModule
    {
        public override void PreInitialize()
        {

            // use HangFire as background job manager.
            Configuration.BackgroundJobs.UseHangfire(configuration =>
            {
                var connectionString = ConfigurationManager.ConnectionStrings["Hangfire.Redis"].ConnectionString;
                configuration.GlobalConfiguration.UseRedisStorage(connectionString,
                    new RedisStorageOptions
                    {
                        Db = 2,
                    });

                configuration.GlobalConfiguration.UseLog4NetLogProvider();

                // 启动Hangfire服务
                var settings = new HangfireSettings();
                var options = new BackgroundJobServerOptions
                {
                    ShutdownTimeout = TimeSpan.FromMinutes(5),  // 关闭定时服务的超时时间
                    ServerName = settings.ServerName, // 服务名称
                    WorkerCount = settings.WorkerCount, // 线程数量
                    Queues = settings.Queues, // 队列名称
                };
                configuration.Server = new BackgroundJobServer(options);
            });
        }

        public override void Initialize()
        {
            //IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}
